const db = require('../db/db')
const {paging} = require('../utils/paging')
const {formatDate} = require('../utils/date')
module.exports = {
    // 获取员工
    GETSTAFFS: async (ctx, next) => {
        const {startPage,endPage} = await paging(ctx)
        const {belongToShop,isDel} = ctx.request.query
         try {
            let [res] = await db.promise().query('select id,staffName,staffSex,staffPhone,staffAge,position,department from staffs where belongToShop=? and isDel=? order by createTime desc limit ?,?',
            [belongToShop,isDel,startPage,endPage])
            let [total] = await db.promise().query('select * from staffs')
            ctx.body = {
                code:200,
                msg:'获取成功',
                staffData:res,
                total:total.length
            }
         } catch (error) {
            ctx.body = {
                code:201,
                msg:'获取失败'+error
            }
         } 
    },
    // 删除员工（恢复员工）
    DELSTAFFS: async (ctx, next) => {
        const {id,isDel} = ctx.request.body
        try {
           let [res] = await db.promise().query('update staffs set isDel=? where id=?',[isDel,id])
           if(res.affectedRows > 0){
            ctx.body = {
                code:200,
                msg:'操作成功'
            }
           }
        } catch (error) {
            ctx.body = {
                code:201,
                msg:'操作失败'
            }
        }
       
    },
    // 添加员工
    ADDSTAFFS: async (ctx, next) => {
        const [id,staffName,staffSex,staffPhone,staffAge,position,department,belongToShop ] = ctx.request.body
        const createTime = new Date()
       try {
        let [res] = await db.promise().query('insert into staffs(id,staffName,staffSex,staffPhone,staffAge,position,department,belongToShop,createTime) values(?,?,?,?,?,?,?,?,?)',
        [id,staffName,staffSex,staffPhone,staffAge,position,department,belongToShop,createTime])
        if(res.affectedRows > 0){
            ctx.body = {
                code:200,
                msg:'添加员工成功'
            }
           }
       } catch (error) {
            ctx.body = {
                code:201,
                msg:'添加员工失败' +error
            }
       }
            
    },
    // 修改员工
    UPDATESTAFFS: async (ctx, next) => {
        const {staffName,staffSex,staffPhone,staffAge,position,department,id} =ctx.request.body
        try {
            let res =await db.promise().query('update staffs set staffName=?,staffSex=?,staffPhone=?,staffAge=?,position=?,department=? where id=?',
            [staffName,staffSex,staffPhone,staffAge,position,department,id])
            if(res.affectedRows !== 0){
                ctx.body = {
                    code:200,
                    msg:"更新成功"
                }
            }else{
                ctx.body = {
                    code:500,
                    msg:"未找到该员工"
                }
            }
        } catch (error) {
            ctx.body = {
                code:500,
                msg:"更新失败"+error
            }
        }
    },
    // 搜索员工
    SEARCHSTAFFS: async (ctx, next) => {
        const {keyWord, belongToShop} = ctx.request.body
        try {
            let [nameRes] = await db.promise().query('select id,staffName,staffSex,staffPhone,staffAge,position,department from staffs where staffName=? and belongToShop=?' ,[keyWord, belongToShop])
            let [iphoneRes] = await db.promise().query('select id,staffName,staffSex,staffPhone,staffAge,position,department from staffs where staffPhone=? and belongToShop=?' ,[keyWord, belongToShop])
            let [partRes] = await db.promise().query('select id,staffName,staffSex,staffPhone,staffAge,position,department from staffs where department=? and belongToShop=?' ,[keyWord, belongToShop])
            if(nameRes.length > 0){
                ctx.body = {
                    code:200,
                    msg:"搜索成功",
                    staff:nameRes
                }
            }else if(iphoneRes.length>0){
                ctx.body = {
                    code:200,
                    msg:"搜索成功",
                    staff:iphoneRes
                }
            }else if(partRes.length>0){
                ctx.body = {
                    code:200,
                    msg:"搜索成功",
                    staff:partRes
                }
            }
            
            else{
                ctx.body = {
                    code:201,
                    msg:"没有该员工",
                }
            }
        } catch (error) {
            ctx.body = {
                code:500,
                msg:"搜索失败" + error
            }
        }
    },
}